//
//  GuideCell.m
//  新特性完成版
//
//  Created by 蔡万鸿 on 15/7/1.
//  Copyright (c) 2015年 黄花菜. All rights reserved.
//

#import "GuideCell.h"
#import "ViewController.h"

@interface GuideCell ()
@property (nonatomic, weak) UIImageView *imgViewBg;
@property (nonatomic, weak) UIButton *startButton;
@end

@implementation GuideCell
// 根据当前也与总页数, 来决定是否需要创建一个"立即体验"的按钮
- (void)setCurrentPageIndex:(NSInteger)index pageCount:(NSInteger)count
{
    if (index == count - 1) {
        if (self.startButton == nil) {
            // 表示到最后一页了, 创建一个立即体验按钮
            UIButton *button = [[UIButton alloc] init];
            [button setBackgroundImage:[UIImage imageNamed:@"guideStart"] forState:UIControlStateNormal];
            
            // 为按钮注册单击事件
            [button addTarget:self action:@selector(startButtonClick) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button];
            self.startButton = button;
        } else {
            // 显示按钮
            self.startButton.hidden = NO;
        }
        
    } else {
        // 如果不是最后一个单元格那么就让按钮隐藏掉
        self.startButton.hidden = YES;
    }
}

// 开始体验app
- (void)startButtonClick
{
    ViewController *mainController = [[ViewController alloc] init];
    
    // 设置TabBarController为UIWindow的根控制器
    self.window.rootViewController = mainController;
}


- (void)setImage:(UIImage *)image
{
    _image = image;
    self.imgViewBg.image = image;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        // 向当前单元格中创建一个UIImageView
        UIImageView *imgViewBg = [[UIImageView alloc] init];
        [self.contentView addSubview:imgViewBg];
        self.imgViewBg = imgViewBg;
    }
    return self;
}

// 设置imgViewBg的大小等于cell的大小
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.imgViewBg.frame = self.bounds;
    CGFloat buttonW = 161;
    CGFloat buttonH = 40;
    CGFloat buttonX = (self.frame.size.width - buttonW) * 0.5;
    CGFloat buttonY = self.frame.size.height * 0.85;
    self.startButton.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
    //[self.contentView bringSubviewToFront:self.startButton];
}

@end
